package com.foxmail.aroundme.boring.module.news.main.ui;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import com.foxmail.aroundme.boring.R;
import com.foxmail.aroundme.boring.adapter.viewpage.NewsChannelTitlePagerAdapter;
import com.foxmail.aroundme.boring.annotation.ActivityFragmentInject;
import com.foxmail.aroundme.boring.base.ui.BaseActivity;
import com.foxmail.aroundme.boring.bean.channel.NewsChannelBean;
import com.foxmail.aroundme.boring.manager.ChannelManager;
import com.foxmail.aroundme.boring.module.news.main.presenter.INewsPre;
import com.foxmail.aroundme.boring.module.news.main.presenter.NewsPreImpl;
import com.foxmail.aroundme.boring.module.news.main.view.INewsView;
import com.foxmail.aroundme.boring.utils.m_enum.RefreshType;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by gzl on 1/15/17.
 *
 */
@ActivityFragmentInject(contentViewId = R.layout.activity_news,
        hasNavigationView = true,
        hasTitleBar = true,
        leftButtonId = R.drawable.icon_meun)
public class NewsActivity extends BaseActivity<INewsPre> implements INewsView, ViewPager.OnPageChangeListener, TabLayout.OnTabSelectedListener{

    private NewsChannelTitlePagerAdapter mPageAdapter;
    private ViewPager mViewPager;
    private TabLayout tabLayout;

    //viewPage滑动结束后的position
    private int lastPosition = -1;
    //当前滑动完成后position
    private int currentPosition = -1;

    private FrameLayout mContent;
    private ImageView mTabMore;
    private View mMoreView;

    @Override
    protected void initView() {
        mPresenter = new NewsPreImpl(this);
        mPresenter.initChannel();

        mContent = (FrameLayout) findViewById(R.id.content_layout);
        mTabMore = (ImageView) findViewById(R.id.tab_more);

    }


    @Override
    public void initViewPager(List<NewsChannelBean> newsChannels) {
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mViewPager.addOnPageChangeListener(this);


        if (newsChannels != null && !newsChannels.isEmpty()) {
            mPageAdapter = new NewsChannelTitlePagerAdapter(getSupportFragmentManager(), newsChannels);
            mViewPager.setAdapter(mPageAdapter);

            lastPosition = 0;
            mViewPager.setCurrentItem(0, false);
            tabLayout.setupWithViewPager(mViewPager);
            tabLayout.setOnTabSelectedListener(this);
            tabLayout.setScrollPosition(0, 0, true);
        } else {
            Toast.makeText(NewsActivity.this, R.string.error_view_no_data, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void initIntentData() {
    }

    @Override
    protected void initListener() {
    }

    @Override
    public void showProgress(@RefreshType.Refresh int type) {

    }

    @Override
    public void hideProgress() {

    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        currentPosition = position;
    }

    @Override
    public void onPageSelected(int position) {
        if (mViewPager == null) {
            return;
        }
        NewsChannelTitlePagerAdapter adapter = (NewsChannelTitlePagerAdapter) mViewPager.getAdapter();
        if (adapter == null) {
            return;
        }

        String title = adapter.getPageTitle(position).toString();
        if (TextUtils.isEmpty(title)) {
            return;
        }
        currentPosition = position;

        lastPosition = currentPosition;
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        if (mContent == null && mMoreView == null || mTabMore == null) {
            return;
        }
        mTabMore.animate().rotation(0).start();
        mContent.removeView(mMoreView);
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    protected void onStop() {
        super.onStop();

        if (mPageAdapter == null) {
            return;
        }

        List<NewsChannelBean> newChannels = new ArrayList<NewsChannelBean>();
        ChannelManager.getInstance().getChannelNews(newChannels);
        if (newChannels.isEmpty()) {
            return;
        }
        List<NewsChannelBean> currentChannels = mPageAdapter.getCurrentChannels();
        if (currentChannels == null || currentChannels.isEmpty()) {
            return;
        }

        newChannels.removeAll(currentChannels);
        if (newChannels.isEmpty()) {
            return;
        }
        for (NewsChannelBean bean : newChannels) {
            mPageAdapter.insertPageFragment(bean);
        }
    }

}
